//
//  HomeViewController.m
//  Jingjing
//
//  Created by Maxwell Rose on 15/11/10.
//  Copyright © 2015年 Mikeshihua. All rights reserved.
//

#import "HomeViewController.h"
#import "ImagePlayerView.h"
#import "BannerModel.h"
#import "BookPageViewController.h"
#import "ProductsDetailViewController.h"
#import "ProductsDetailsInsidePagesViewController.h"
@interface HomeViewController ()<ImagePlayerViewDelegate>

@property (weak, nonatomic) IBOutlet ImagePlayerView *imagePlayerView;

@property (strong, nonatomic) NSMutableArray *bannerDataSources;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.bannerDataSources = [NSMutableArray array];
    [self setupImagePlayerView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.navigationController.navigationBarHidden = YES;
    [self refresh];

}

- (void)viewWillDisappear:(BOOL)animated{
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

}

- (void)refresh{
    [AppGlobalClass PostDataWithNSstring:kLinkOfBannerList withDictionary:@{} success:^(NSDictionary *dictionary) {
        if ([dictionary[@"success"] isEqualToNumber:@YES]) {
            [self.bannerDataSources removeAllObjects];
            NSArray *arr = [[[dictionary objectForKey:@"responseData"] firstObject]objectForKey:@"rows"];
            for (NSDictionary *dic in arr) {
                BannerModel *model = [[BannerModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [_bannerDataSources addObject:model];
            }
            if (_bannerDataSources.count > 0) {
                [self.imagePlayerView reloadData];
            }
            else{
                for (int i = 0; i < 5; i++) {
                    BannerModel *model = [[BannerModel alloc]init];
                    [_bannerDataSources addObject:model];
                }
                [self.imagePlayerView reloadData];
                
            }
            
        }
        
    } failure:^(NSError *error) {
        [_bannerDataSources removeAllObjects];
        if (_bannerDataSources.count == 0) {
            for (int i = 0; i < 5; i++) {
                BannerModel *model = [[BannerModel alloc]init];
                [_bannerDataSources addObject:model];
            }
            [self.imagePlayerView reloadData];
        }
        
    } networkerror:^(NSString *networkerror) {
        
        [_bannerDataSources removeAllObjects];
        if (_bannerDataSources.count == 0) {
            for (int i = 0; i < 5; i++) {
                BannerModel *model = [[BannerModel alloc]init];
                [_bannerDataSources addObject:model];
            }
            [self.imagePlayerView reloadData];
        }
        
        
    }];
}
/**
 *  设置轮播图
 */
- (void)setupImagePlayerView{
    //    self.imagePlayerView = [[ImagePlayerView alloc]init];
    self.imagePlayerView.imagePlayerViewDelegate = self;
    // set auto scroll interval to x seconds
    self.imagePlayerView.scrollInterval = 3.0f;
    // adjust pageControl position
    self.imagePlayerView.pageControlPosition = ICPageControlPosition_BottomCenter;
    // hide pageControl or not
    self.imagePlayerView.hidePageControl = NO;
    // adjust edgeInset
    //    self.imagePlayerView.edgeInsets = UIEdgeInsetsMake(10, 20, 30, 40);
    [self.imagePlayerView reloadData];
}
#pragma mark - Image Player View Delegate
- (NSInteger)numberOfItems
{
    return self.bannerDataSources.count;
}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView loadImageForImageView:(UIImageView *)imageView index:(NSInteger)index
{
    // recommend to use SDWebImage lib to load web image
    BannerModel *model = self.bannerDataSources[index];
    NSURL *imgURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",RESOURCE_URL,model.imgURL]];
    
    imageView.contentMode = UIViewContentModeCenter;
    imageView.backgroundColor = [UIColor hexStringToColor:@"#E5E5E5"];
    [imageView sd_setImageWithURL:imgURL placeholderImage:[UIImage imageNamed:@"img_default"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            imageView.contentMode = UIViewContentModeScaleToFill;

        }
    }];
//    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Anne.jpg"]];
    

}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView didTapAtIndex:(NSInteger)index
{
    NSLog(@"did tap index = %d", (int)index);
    BannerModel *model = _bannerDataSources[index];
    NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,model.objType);
    if ([model.objType isEqualToNumber:@1]) {
        NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,@"类型1，杂志");
        BookPageViewController *bookPageViewCon = [[BookPageViewController alloc]initWithBookID:model.objItemId];
        bookPageViewCon.title = model.objItemTitle;
        [self.navigationController pushViewController:bookPageViewCon animated:YES];
        bookPageViewCon.navigationController.navigationBarHidden = NO;
        bookPageViewCon.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_back"] style:(UIBarButtonItemStylePlain) target:nil action:nil];
    }else if ([model.objType isEqualToNumber:@2]){
        NSLog(@" %d | %s | %@",__LINE__,__FUNCTION__,@"类型2，活动");
        ProductsDetailsInsidePagesViewController *productDetailVC = [[ProductsDetailsInsidePagesViewController alloc]init];
        productDetailVC.activitesTypeId = model.objItemId;
        productDetailVC.navTitle = model.objItemTitle;
        [self.navigationController pushViewController:productDetailVC animated:YES];
        
    }
    NSLog(@" %d | %s | %@ %@",__LINE__,__FUNCTION__,model.objItemTitle,model.objItemId);

}


@end
